package com.mjfeed.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.ProductCategory;
import com.mjfeed.service.ProductCategoryService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 商品分类
 * @author zhaominglei
 * @date 2015年1月7日
 */
@Controller("adminProductCategoryController")
@RequestMapping("/admin/productCategory")
public class ProductCategoryController extends BaseController {
	@Resource(name="productCategoryService")
	private ProductCategoryService productCategoryService;
	
	@RequestMapping(value = "/list" ,method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/productCategory/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<ProductCategory> pagination = productCategoryService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/productCategory/list";
	}
	
	@RequestMapping(value = "/add" ,method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("productCategoryTree", productCategoryService.findTree());
		return "admin/productCategory/add";
	}
	
	@RequestMapping(value = "/save" ,method=RequestMethod.POST)
	public String save(ProductCategory productCategory, HttpServletRequest request, HttpServletResponse response, Model model){
		if (productCategory == null) {
			return ERROR_VIEW;
		}
		String parentId = request.getParameter("parentId");
		int grade = 0;
		if (parentId != null && !parentId.equals("")) {
			ProductCategory parentCategory =  productCategoryService.getById(ProductCategory.class, Long.valueOf(parentId));
			StringBuilder treePath = new StringBuilder();
			treePath.append(parentCategory.getTreePath());
			treePath.append(parentCategory.getId()).append(ProductCategory.TREE_PATH_SEPARATOR);
			productCategory.setTreePath(treePath.toString());
			grade = parentCategory.getGrade().intValue();
			grade++;
			productCategory.setParent(parentCategory);
		} else {
			productCategory.setTreePath(ProductCategory.TREE_PATH_SEPARATOR);
		}
		productCategory.setGrade(grade);
		productCategoryService.build(productCategory);
		productCategoryService.save(productCategory);
		return "redirect:/admin/productCategory/list.shtml";
	}
	
	@RequestMapping(value = "/edit" ,method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id.intValue() < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("category",  productCategoryService.getById(ProductCategory.class, id));
		request.setAttribute("productCategoryTree", productCategoryService.findTree());
		return "admin/productCategory/edit";
	}
	
	@RequestMapping(value = "/update" ,method=RequestMethod.POST)
	public String update(ProductCategory productCategory, HttpServletRequest request, HttpServletResponse response, Model model){
		if (productCategory == null || productCategory.getId() == null || productCategory.getId() < 1) {
			return ERROR_VIEW;
		}
		ProductCategory pCategory =  productCategoryService.getById(ProductCategory.class, productCategory.getId());
		pCategory.setName(productCategory.getName());
		String parentId = request.getParameter("parentId");
		int grade = 0;
		if (parentId != null && !parentId.equals("")) {
			ProductCategory parentCategory =  productCategoryService.getById(ProductCategory.class, Long.valueOf(parentId));
			StringBuilder treePath = new StringBuilder();
			treePath.append(parentCategory.getTreePath());
			treePath.append(parentCategory.getId()).append(ProductCategory.TREE_PATH_SEPARATOR);
			pCategory.setTreePath(treePath.toString());
			grade = parentCategory.getGrade().intValue();
			grade++;
			pCategory.setParent(parentCategory);
		} else {
			pCategory.setTreePath(ProductCategory.TREE_PATH_SEPARATOR);
		}
		pCategory.setGrade(grade);
		pCategory.setSeoTitle(productCategory.getSeoTitle());
		pCategory.setSeoKeywords(productCategory.getSeoKeywords());
		pCategory.setSeoDescription(productCategory.getSeoDescription());
		pCategory.setOrders(productCategory.getOrders());
		productCategoryService.build(productCategory);
		pCategory.setSource((productCategory.getSource() == null || productCategory.getSource().equals("") ? pCategory.getSource() : productCategory.getSource()));
		productCategoryService.update(pCategory);
		return "redirect:/admin/productCategory/list.shtml";
	}
}
